package edu.uoc.tds.pac4.client.ss4;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
//import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import edu.uoc.tds.pac4.beans.Event;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;

/**
 * JScrollPane amb la llista d'events
 * @author Guillem SS2 
 */
public class JPanelEvents extends JPanel {
	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPaneEvents = null;
	protected JTable jTableEvents = null;
	protected DefaultTableModel tableModel;

	/**
	 * Constructor
	 * This method initializes JPanelEvents
	 * @return javax.swing.JPanel
	 */
	public JPanelEvents() {
		GridBagConstraints gbcJScrollPaneEvents = new GridBagConstraints();
		gbcJScrollPaneEvents.fill = GridBagConstraints.BOTH;
		gbcJScrollPaneEvents.weighty = 1.0;
		gbcJScrollPaneEvents.weightx = 1.0;
		this.setLayout(new GridBagLayout());
		this.setName("jPanelEvents");
		this.add(getJScrollPaneEvents(), gbcJScrollPaneEvents);
	}
	
	/**
	 * This method initializes jScrollPanelEvents
	 * 
	 * @return javax.swing.JPanelEvents
	 */
	private JScrollPane getJScrollPaneEvents() {
		if (jScrollPaneEvents== null) {
			jScrollPaneEvents = new JScrollPane();
			jScrollPaneEvents.setViewportView(getJTableEvents());
		}
		return jScrollPaneEvents;
	}
	
	/**
	 * Dibuixa els events de la taula
	 * @param currentEvents
	 */
	public void drawEvents(Vector<Event> currentEvents)
	{
		// Clear table
		int numrows = tableModel.getRowCount();
		for (int i = numrows - 1; i >= 0; i--)
			tableModel.removeRow(i);
		// actualitza la taula
		if(currentEvents != null) {
			Iterator<Event> it = currentEvents.iterator();
			while (it.hasNext()) {
				addRow(it.next());
			}
		}
	}
	
	/**
	 * This method initializes jTableEvents
	 * 
	 * @return javax.swing.JTable
	 */
	protected JTable getJTableEvents() {
		if (jTableEvents == null) {
			tableModel = new DefaultTableModel();

			jTableEvents = new JTable(tableModel) {
				private static final long serialVersionUID = 1L;

				public boolean isCellEditable(int rowIndex, int vColIndex) {
					return false;
				}
			};
			tableModel.addColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.EventNom"));
			tableModel.addColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.EventData"));
			tableModel.addColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.EventDataFi"));
			tableModel.addColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.EventTipus"));
			tableModel.addColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.Centre"));
						
			// Ample de columnes
			TableColumn tableColumn;
			tableColumn = jTableEvents.getColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.EventNom"));
			tableColumn.setPreferredWidth(300);
			tableColumn.setMinWidth(200);
			tableColumn = jTableEvents.getColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.EventData"));
			tableColumn.setPreferredWidth(70);
			tableColumn.setMinWidth(70);
			tableColumn.setMaxWidth(80);
			tableColumn = jTableEvents.getColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.EventDataFi"));
			tableColumn.setPreferredWidth(70);
			tableColumn.setMinWidth(70);
			tableColumn.setMaxWidth(80);
			tableColumn = jTableEvents.getColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.EventTipus"));
			tableColumn.setPreferredWidth(100);
			tableColumn.setMinWidth(50);
			tableColumn.setMaxWidth(130);
			tableColumn = jTableEvents.getColumn(TDSLanguageUtils
					.getMessage("Client.PantallaLListarEvents.Centre"));
			tableColumn.setPreferredWidth(220);
			tableColumn.setMinWidth(100);
			tableColumn.setMaxWidth(230);

		}
		return jTableEvents;
	}
	
	/**
	 * Afegeix un event a la taula
	 * @param event Event
	 */
	private void addRow(Event event) {
		Vector<String> row = new Vector<String>();
		// resalta els events donats de baixa
		row.add((event.isActiu() ? "" : "(" +
				TDSLanguageUtils.getMessage("Client.PanelEvents.Baixa") + ") ") + event.getDescripcio());
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		row.add(formatter.format((event.getDataInici())));
		row.add(formatter.format((event.getDataFi())));
		row.add(event.getTipusevent().getNom());
		row.add(event.getCentre().getNom());
		row.add(String.valueOf(event.getDurada()));

		tableModel.addRow(row);
	}
	
	/**
	 * Remove row
	 * @param row row
	 */
	public void removeSelectedRow(int row)
	{
		tableModel.removeRow(row);
	}

	/**
	 * Retorna l'index de la fila seleccionada
	 * @return index de la fila
	 */
	public int getSelectedRow() {
		return jTableEvents.getSelectedRow();
	}
}
